﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Entity.IRepository.Project;
using Entity.ProjectEnt;
using Logic.Repository.Project;
using ZFine.Code;

namespace ZFine.Application.Project
{
    public class CarriageInfoLogApp
    {
        private ICarriageInfoLogRepository service = new CarriageInfoLogRepository();
        public List<CarriageInfoLogEntity> GetList()
        {
            var expression = ExtLinq.True<CarriageInfoLogEntity>();
            expression = expression.And(t => t.State == true);
            return service.IQueryable(expression).ToList();
        }
        public List<CarriageInfoLogEntity> GetList(Expression<Func<CarriageInfoLogEntity, bool>> predicate)
        {
            return service.IQueryable(predicate).ToList();
        }
        public List<CarriageInfoLogEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<CarriageInfoLogEntity>();
            expression = expression.And(t => t.State == true);
            return service.FindList(expression, pagination);
        }
        public List<CarriageInfoLogEntity> GetListArrive(Pagination pagination, string keyword, string StartTime, string EndTime, string carId, string customerName)
        {
            var expression = ExtLinq.True<CarriageInfoLogEntity>();
            //if (!string.IsNullOrEmpty(keyword))
            //{
            //    expression = expression.And(f => f.PathWay.Contains(keyword));
            //}
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(f => f.ThingsName.Contains(keyword));
            }
            if (!string.IsNullOrEmpty(carId))
            {
                expression = expression.And(f => f.CarriageID.Contains(carId));
            }
            if (!string.IsNullOrEmpty(customerName))
            {
                expression = expression.And(f => f.Customers.Contains(customerName));
            }
            if (!string.IsNullOrEmpty(StartTime))
            {
                DateTime sTime = Convert.ToDateTime(StartTime);
                if (!string.IsNullOrEmpty(EndTime))
                {
                    DateTime eTime = Convert.ToDateTime(EndTime);
                    expression = expression.And(f => f.ArriveTime >= sTime);
                    expression = expression.And(f => f.ArriveTime < eTime);
                }
                else
                {
                    expression = expression.And(f => f.ArriveTime > sTime);
                }

            }
            else
            {
                if (!string.IsNullOrEmpty(EndTime))
                {
                    DateTime eTime = Convert.ToDateTime(EndTime);
                    expression = expression.And(f => f.ArriveTime < eTime);
                }
            }
            //expression = expression.And(t => t.State == true);
            return service.FindList(expression, pagination);
        }

        public void DeleteFormByCarcode(string carCode)
        {
            var expression = ExtLinq.True<CarriageInfoLogEntity>();
            expression = expression.And(t => t.CarriageID == carCode);
            service.Delete(expression);
        }

        public List<CarriageInfoLogEntity> GetListD(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<CarriageInfoLogEntity>();
            expression = expression.And(t => t.State == true);
            return service.FindList(expression, pagination);
        }
        public CarriageInfoLogEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public int DeleteForm(string keyValue)
        {
            var local = service.FindEntity(keyValue);
            if (local != null)
            {
                local.State = false;
                return service.Update(local);
            }
            else
            {
                return -1;
            }
        }
        public int SubmitForm(CarriageInfoLogEntity ServiceEntity, string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (LoginInfo != null)
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    ServiceEntity.Modify(keyValue);
                    //return service.UpdateN(ServiceEntity);
                }
                else
                {
                    ServiceEntity.Create();
                    ServiceEntity.State = true;
                    return service.Insert(ServiceEntity);
                }
            }
            return -1;
        }

    }
}
